package littlecyberwar.ui;

import javax.swing.*;
import java.beans.*; //Property change stuff
import java.awt.*;
import java.awt.event.*;
import java.util.*;

import littlecyberwar.util.*;
import littlecyberwar.model.*;

/**
 * Create a DieRoll modal dialog box
 */
public class DieRollDialog extends JDialog {
  public static final String _TITLE="Die Roll Utility".intern();
  /**
   * Number of face on he die to be rolled
   */
  private static JComboBox dieFaceCB;
  private static String[] dieFaceVals = {"4", "6", "8", "10", "12", "20", "100"};
  /**
   * Number of dice to roll
   */
  private static JComboBox dieNoCB;
  private static String[] dieNoVals = {"1","2", "3", "4", "5", "6", "7", "8", "9", "10"};
  private static JLabel dieTypeLab = new JLabel("Die Type: ");
  private static JLabel dieNoLab = new JLabel("No. of Dice: ");
  private static JButton rollBut = new JButton("roll");
  private static boolean isVisible = false;

  private JLabel outLabel;
  private InternalCmdFacade cmdFacade;

  public DieRollDialog( Frame fr, InternalCmdFacade mod  ) {
    super( fr, _TITLE, true );
    cmdFacade = mod;
    JPanel panel = new JPanel();
    JPanel inputPanel = new JPanel();

    dieFaceCB= new JComboBox(dieFaceVals);
    dieFaceCB.setSelectedIndex(1);  // select D6 to begin with

    dieNoCB = new JComboBox(dieNoVals);
    dieNoCB.setSelectedIndex(0);  // select one die to begin with

    inputPanel.setLayout(new GridLayout(2,2,3,3));
    inputPanel.add(dieTypeLab);
    inputPanel.add(dieFaceCB);
    inputPanel.add(dieNoLab);
    inputPanel.add(dieNoCB);

    // now do the Big Panel
    panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
    panel.add(inputPanel);
    panel.add(rollBut);
    this.getContentPane().add(panel);
    setResizable(false);

    // setup focus traversal
    dieFaceCB.setNextFocusableComponent(dieNoCB);
    dieNoCB.setNextFocusableComponent(rollBut);
    rollBut.setNextFocusableComponent(dieFaceCB);

    // setup output printing ==> status bar
    outLabel = Util.getStatusBar();

    // setup action with the roll button
    rollBut.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        Vector v = getDieRollResults();
        StringBuffer sb = new StringBuffer("Die Roll result= ");
        for (int i=0; i< v.size(); i++ ) {
          sb.append(v.elementAt(i));
          if ( i < v.size()-1){
          	sb.append(", ");
          }
        }
        DiceCommand cmd = new DiceCommand(sb.toString());
        cmd.setPlayerName(Players.getSelfName());
		cmdFacade.addCmd(cmd);
        // outLabel.setText(sb.toString());
        hide();
      }
    });
  }

  public Vector getDieRollResults() {
    String faceStr = (String)dieFaceCB.getSelectedItem();
    String numStr = (String)dieNoCB.getSelectedItem();
    int face = Integer.parseInt(faceStr);
    int num = Integer.parseInt(numStr);
    Vector v = Dice.getRolls(face, num);
    return ( v );
  }
}